package com.bjpowernode.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.bjpowernode.config.listener.ClueDataListener;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TActivity;
import com.bjpowernode.entity.TClue;
import com.bjpowernode.entity.TClueDO;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.query.BaseQuery;
import com.bjpowernode.query.ClueQuery;
import com.bjpowernode.service.ClueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {

    @Resource
    private TClueMapper tClueMapper;

    @Override
    public PageInfo<TClue> getCluePage(Integer current) {
        //1.设置PageHelper (第一个参数是查第几页，第二个参数每页查多少条数据)
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        //2.查询数据库表的数据
        List<TClue> list = tClueMapper.selectCluePage(new BaseQuery());

        //3.封装分页数据到PageInfo
        PageInfo<TClue> info = new PageInfo<>(list);

        //4.返回PageInfo对象
        return info;
    }

    @Override
    public void importExcel(InputStream is) {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(is, TClueDO.class, new ClueDataListener(tClueMapper)).sheet().doRead();
    }

    @Override
    public int checkPhone(String phone) {
        return tClueMapper.selectCountByPhone(phone);
    }

    @Override
    public int saveClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();
        //对象属性值的复制，把activityQuery对象里面的属性值复制到tActivity对象的属性中
        //复制的条件：源对象 和 目标对象 里面的属性字段名要相同，属性的字段类型也相同，才可以复制
        BeanUtils.copyProperties(clueQuery, tClue);
        //创建时间
        tClue.setCreateTime(new Date());
        //创建人
        TUser authUser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tClue.setCreateBy(currentLoginUserId);
        return tClueMapper.insertSelective(tClue);
    }

    @Override
    public TClue getClueById(Integer clueId) {
        return tClueMapper.selectById(clueId);
    }

    @Override
    public int updateClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();
        //对象属性值的复制，把activityQuery对象里面的属性值复制到tActivity对象的属性中
        //复制的条件：源对象 和 目标对象 里面的属性字段名要相同，属性的字段类型也相同，才可以复制
        BeanUtils.copyProperties(clueQuery, tClue);
        //编辑时间
        tClue.setEditTime(new Date());
        //编辑人
        TUser authUser = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer currentLoginUserId = authUser.getId();
        tClue.setEditBy(currentLoginUserId);
        return tClueMapper.updateByPrimaryKeySelective(tClue);
    }

    @Override
    public int delClueById(Integer clueId) {
        return tClueMapper.deleteByPrimaryKey(clueId);
    }

    @Override
    public int batchDelClueByIds(List<String> idList) {
        return tClueMapper.deleteByIdList(idList);
    }
}
